$(function () {
    //获取用户基本信息
    getUserInfo();
    //退出登录
    $('#loginOut').on('click',function(){        
        window.localStorage.removeItem('token');
        location.href = '/login.html'
    })
})

//获取用户基本信息
function getUserInfo() {
    $.ajax({
        type: 'get',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: window.localStorage.getItem('token') || ''
        // },
        success(res) {
            console.log(res);            
            if (res.status !== 0) { 
                location.href = '/login.html'               
                return layui.layer.msg('获取用户信息失败!');
                
            }
            //渲染用户图像;
            var username = res.data.nickname || res.data.username;
            if (!res.data.user_pic) {                
                $(".userinfo img").hide();
                $(".userinfo .text-logo").show();
                $(".text-logo").html(res.data.username.toUpperCase().substr(0, 1));
                $(".welc-user").html('欢迎&nbsp&nbsp' + username);
                
            } else {
                $(".userinfo img").show();
                $(".userinfo .text-logo").hide();
                $(".userinfo img").attr('src',res.data.user_pic);
                $(".welc-user").html('欢迎&nbsp&nbsp' + username);
               
            }
        },
        complete(res){//无论请求成功失败都会执行
            console.log(res);
            if(res.responseJSON.status === 1 && res.responseJSON.message === '无效的token'){
                window.localStorage.removeItem('token');
                location.href = '/login.html'
            }
        }
    })
}